﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Picnic.Spec.Console
{
	class Program
	{
		private const string AssemblyKey = "assembly";

		static void Main(string[] args)
		{
			var asseblyFilePath = GetAsseblyFilePath(args);
			if (string.IsNullOrEmpty(asseblyFilePath))
			{
				System.Console.WriteLine("Assembly is not specified.");
				System.Console.ReadLine();
				return;
			}

			var assembly = Assembly.LoadFrom(asseblyFilePath);
			var types = assembly.GetTypes();
			var specificationTypes = types.Where(type => type.IsPublic && type.IsSubclassOf(typeof(Specification)));
			var listener = new ConsoleListener();
			var specificationManager = new SpecificationManager();

			specificationManager.Add(specificationTypes);
			specificationManager.ValidateStatements(listener);

			System.Console.ReadLine();
		}

		private static string GetAsseblyFilePath(IEnumerable<string> args)
		{
			const string key = "/" + AssemblyKey + ":";
			var result = args.Where(arg => arg.StartsWith(key, StringComparison.OrdinalIgnoreCase))
				.Select(arg => arg.Substring(key.Length))
				.Select(value => value.Trim(new [] { '"', '\'' }))
				.FirstOrDefault();

			return result ?? string.Empty;
		}
	}
}
